package com.animee.day11.slice;

import com.animee.day11.ResourceTable;
import com.animee.day11.bean.FilmInfoBean;
import com.animee.day11.bean.FilmTypeBean;
import com.animee.day11.utils.FilmGridProvider;
import com.animee.day11.utils.FilmInfoProvider;
import com.animee.day11.utils.HttpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class FilmListAbilitySlice extends AbilitySlice implements Component.ClickedListener, ListContainer.ItemClickedListener {
    ListContainer lc,lc2;
    Text typeTv;
    Image changeIv;
    List<FilmInfoBean>mDatas = new ArrayList<>();
    private FilmInfoProvider provider;
    private String url_start = "https://movie.douban.com/j/chart/top_list?type=";
    private String url_end = "&interval_id=100%3A90&action=&start=0&limit=20";
    private String url ;
    boolean isList = true;   //判断显示的类型
    private FilmGridProvider gridProvider;
    private TableLayoutManager gridManager;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        super.setUIContent(ResourceTable.Layout_abilitiy_filmlist);
        initView();
        //获取上一个页面传递的数据
        FilmTypeBean.FilmTypesDTO dto = intent.getSerializableParam("film");
        typeTv.setText(dto.getName());
        int id = dto.getId();   //得到类别 id
        url = url_start+id+url_end;
        getNetworkData();  //获取网络数据

    }
    //获取网络数据
    private void getNetworkData() {
        //开启线程下载网络数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                String json = HttpUtils.getJsonByNetwork(url);
                Gson gson = new Gson();
                Type type = new TypeToken<List<FilmInfoBean>>(){}.getType();
                List<FilmInfoBean>list =  gson.fromJson(json,type);
                mDatas.addAll(list);
                //回到主线程,进行UI更新
                getMainTaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        provider.notifyDataChanged();
                        gridProvider.notifyDataChanged();
                    }
                });
            }
        }).start();
    }


    private void initView() {
        typeTv = findComponentById(ResourceTable.Id_flist_tv_type);
        changeIv = findComponentById(ResourceTable.Id_flist_iv);
        changeIv.setClickedListener(this);
        lc = findComponentById(ResourceTable.Id_flist_lc);
        lc2 = findComponentById(ResourceTable.Id_flist_lc2);
        //设置一行显示几列的设置

        gridManager = new TableLayoutManager();
        gridManager.setColumnCount(3);
        lc2.setLayoutManager(gridManager);

        provider = new FilmInfoProvider(this, mDatas);
        gridProvider = new FilmGridProvider(this, mDatas);
        lc.setItemProvider(provider);
        lc2.setItemProvider(gridProvider);
        //设置单项点击事件
        lc.setItemClickedListener(this);
        lc2.setItemClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    //点击事件
    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_flist_iv) {
            isList = !isList;
            if (isList){    //是列表类型的
                changeIv.setPixelMap(ResourceTable.Media_icon_more);
                lc.setVisibility(Component.VISIBLE);
                lc2.setVisibility(Component.HIDE);
            }else{    //是网格类型的
                changeIv.setPixelMap(ResourceTable.Media_icon_nav);
                lc2.setVisibility(Component.VISIBLE);
                lc.setVisibility(Component.HIDE);
            }
        }
    }

    @Override
    public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
        FilmInfoBean bean = mDatas.get(i);
        Intent intent = new Intent();
        intent.setParam("film",bean);
        present(new FilmInfoAbilitySlice(),intent);
    }
}
